﻿
/*
 * Copyright (C) Han Wei
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Thrift.Transport;
using Thrift.Protocol;
using BSvr.Model;

namespace BSvr.Client
{
    /// <summary>
    /// 客户端
    /// </summary>
    public class Client
    {
        TTransport          _transport;
        TProtocol           _protocol;
        BatchChannel.Client _client;

        public Client(string ip, int port)
        {
            _transport = new TSocket(ip, port);
            _protocol = new TBinaryProtocol(_transport);
            _client = new BatchChannel.Client(_protocol);
        }

        /// <summary>
        /// 打开连接
        /// </summary>
        public void Open()
        {
            _transport.Open();
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        public void Close()
        {
            _transport.Close();
        }
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="handlerno">业务编号</param>
        /// <param name="cmd">命令</param>
        /// <param name="cmdarg">命令参数</param>
        /// <returns>命令执行结果{true,业务定}{false,"失败原因"}</returns>
        /// <remarks>
        /// 失败原因
        /// <?xml version="1.0"?><error><code>错误编码</code><content>错误说明</content></error>
        /// </remarks>
        public OpRst<string> Excute(string handlerno, string cmd, List<string> cmdarg)
        {
            Result result = _client.Excute(handlerno, cmd, cmdarg);
            return new OpRst<string>(result.IsSuccess, result.OpResult);
        }
        /// <summary>
        /// 查询方法
        /// </summary>
        /// <param name="handlerno">业务编号</param>
        /// <param name="query">查询</param>
        /// <param name="queryarg">查询参数</param>
        /// <returns>查询结果以xml表示{true,结果}{false,失败原因}</returns>
        /// <remarks>
        /// 失败原因
        /// <?xml version="1.0"?><error><code>错误编码</code><content>错误说明</content></error>
        /// </remarks>
        public OpRst<string> Query(string handlerno, string query, List<string> queryarg)
        {
            Result result = _client.Query(handlerno, query, queryarg);
            return new OpRst<string>(result.IsSuccess, result.OpResult);
        }
    }
}
